package Yangtz.Cs.Manager.Controller;

import Yangtz.Cs.Entity.BookBean;
import Yangtz.Cs.Service.BookService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/modifyBookServlet")
public class modifyBookServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = response.getWriter();


        //判断是否多段数据.（只有是多段的数据，才是文件上传的）
        if (ServletFileUpload.isMultipartContent(request)) {
            //创建 FileItemFactory 工厂实现类
            FileItemFactory fileItemFactory = new DiskFileItemFactory();
            //.创建用于解析上传数据的工具类
            ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
            try {
                List<FileItem> fileItems = servletFileUpload.parseRequest(request);
                BookBean book = new BookBean();

                for (FileItem fileItem : fileItems) {
                    //属性名，及key名字
                    String fieldName = fileItem.getFieldName();
                    switch (fieldName) {
                        case "bkID":
                            book.setBkID(fileItem.getString("UTF-8"));
                            break;
                        case "bkName":
                            book.setBkName(fileItem.getString("UTF-8"));
                            break;
                        case "bkAuthor":
                            book.setBkAuthor(fileItem.getString("UTF-8"));
                            break;
                        case "bkPress":
                            book.setBkPress(fileItem.getString("UTF-8"));
                            break;
                        case "bkPrice":
                            book.setBkPrice(fileItem.getString("UTF-8"));
                            break;
                        case "bkBrief":
                            book.setBkBrief(fileItem.getString("UTF-8"));
                            break;
                        default: {
                            throw new IllegalStateException("Unexpected value: " + fieldName);
                        }
                    }
                }
                BookService bookService = new BookService();
                if (bookService.modifyBook(book)) {
                    writer.println("修改书籍成功");
                } else {
                    writer.println("修改书籍失败");
                }
            } catch (FileUploadException e) {
                e.printStackTrace();
            }

        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
